

package com.masiyi.springsecuritydemo.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class BasicController {

    // http://127.0.0.1:8080/hello?name=lisi
//    @RequestMapping("/hello")
//    public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
//        return "Hello " + name;
//    }


    @RequestMapping("/auth")
    public String hello() {
        //org.springframework.security.web.context.HttpRequestResponseHolder 关键方法
        Authentication authentication = SecurityContextHolder
                .getContext().getAuthentication();
        org.springframework.security.core.userdetails.User principal = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
        System.out.println("身份 :"+principal.getUsername());
        System.out.println("凭证 :"+authentication.getCredentials());
        System.out.println("权限 :"+authentication.getAuthorities());
        return "hello security";
    }




}
